//
//  MineSmallCollectView.m
//  礼物go
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineSmallCollectView.h"
#import "HotDetailModel.h"


#define MARGINSPACE 20
#define ITEMHEIGHT 200

@implementation MineSmallCollectView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray array];
       // NSLog(@"11111111111");
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDataArray:) name:@"hotcollData" object:nil];
        
        
        
//        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//        
//        NSData *hotData = [user objectForKey:@"hotColumnData"];
        
      //  self.dataArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:hotData]];
        
    
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake((SCREENWIDTH - MARGINSPACE * 3) / 2, ITEMHEIGHT);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.sectionInset = UIEdgeInsetsMake(MARGINSPACE, MARGINSPACE , 0, MARGINSPACE);
        
        self.smallCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 44) collectionViewLayout:flowLayout];
        self.smallCollectionView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.smallCollectionView];
        self.smallCollectionView.dataSource = self;
        self.smallCollectionView.delegate = self;
        [self.smallCollectionView registerClass:[HotColumnCell class] forCellWithReuseIdentifier:@"smallCollectionCell"];
        [self.smallCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"smallNothingCell"];
        
        self.smallCollectionView.showsVerticalScrollIndicator = NO;
    self.smallCollectionView.alwaysBounceVertical = YES;//让collectionView始终可以垂直滑动;
        
        
        
        // 注册滑动到顶部的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"goTop" object:nil];
        
        // 注册离开顶部的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"leaveTop" object:nil];
    }

    return self;
}

// 接收通知
-(void)notificationAction:(NSNotification *)notification{
    // 如果是滑动到顶部通知
    NSString *notificationName = notification.name;
    if ([notificationName isEqualToString:@"goTop"]) {
        NSDictionary *userInfo = notification.userInfo;
        NSString *canScroll = userInfo[@"canScroll"];
        if ([canScroll isEqualToString:@"1"]) {
            self.canScroll = YES;
            self.smallCollectionView.showsVerticalScrollIndicator = NO;
        }
    }
    
    if ([notificationName isEqualToString:@"leaveTop"]) {
        // 离开顶部,不允许小的动了
        self.smallCollectionView.contentOffset = CGPointZero;
        self.canScroll = NO;
        self.smallCollectionView.showsVerticalScrollIndicator = NO;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 如果不能动则保持(0,0)的偏移量
    if (!self.canScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    CGFloat offsetY = scrollView.contentOffset.y;
    //NSLog(@"small colect ------%f",offsetY);
    if (offsetY <= 0) {
        
        // 发出离开置顶标题位置
        [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil userInfo:@{@"canScroll":@"1"}];
    }
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArray.count != 0) {
        HotDetailModel *hotDModel = [self.dataArray objectAtIndex:indexPath.row];
        
        
        HotColumnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallCollectionCell" forIndexPath:indexPath];
        
        cell.hotDeModel = hotDModel;
        
        
        //cell.backgroundColor = [UIColor purpleColor];
        //    HotDetailModel /
        return cell;

    } else {
        UICollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"smallNothingCell" forIndexPath:indexPath];
    
        
        
        return cell;
    }
    
 }

//接受通知
-(void)getDataArray:(NSNotification *)notification{

    self.dataArray = [notification object];
    [self.smallCollectionView reloadData];
    //NSLog(@"%@",_dataArray);



}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.dataArray.count != 0) {
        return self.dataArray.count;
    } else {
        return 1;
    }
    
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HotDetailModel *hotModel = [self.dataArray objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hotColumnPush" object:hotModel.myId];

}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
